package com.itheima.controller;

import com.itheima.domain.Account;
import com.itheima.domain.Infor;
import com.itheima.domain.Trade;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user")
@ResponseBody
public class UserController {

    @Autowired
    private UserService userService;
    @RequestMapping("/login")
    public Infor login(@RequestBody Account account , HttpSession session, String check, HttpServletResponse response, HttpServletRequest request) throws IOException, ServletException {
        String o = (String) session.getAttribute("check");
        Account acc=null;
        session.removeAttribute("check");
        Infor infor = new Infor();
        if (check.equalsIgnoreCase(o)){
            acc = userService.login(account);
        }else {
            infor.setFalg(false);
            infor.setError("验证码错误");
            return infor;
        }
        if (acc!=null){
            infor.setFalg(true);
            return infor;
        }
        infor.setFalg(false);
        infor.setError("用户名或密码错误");
        return infor;
    }

    @RequestMapping("/selectBalance")
    public String selectBalance( String password, String accountID){
        String remaining = userService.selectBalance(accountID,password);
        return remaining;
    }

    @RequestMapping("/selectTransactions")
    public Infor selectTransactions(@DateTimeFormat(pattern = "yyyy-MM-dd")Date startTime,@DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime, String accountID){
        SimpleDateFormat sim = new SimpleDateFormat();
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.MONTH,-1);
        Date lastOneMonthTime = c.getTime();
        Infor infor = new Infor();
        if (startTime.getTime()>=lastOneMonthTime.getTime()){
            List<Trade> trade = userService.selectTransactions(startTime,endTime,accountID);
            if (trade!=null&&trade.size()>0){
                infor.setFalg(true);
                infor.setTrade(trade);
                return infor;
            }
        }
        infor.setFalg(false);
        infor.setError("请选择正确的时间段");
        return infor;
    }



    @RequestMapping("/transferTo")
    public Infor transferTo(String accountID,String password,String toAccount,String money){
       Infor infor =  userService.transferTo(accountID,password,toAccount,money);
        return infor;
    }
}
